package cn.icare.cms.controller.restful;

import cn.icare.cms.MenuService;
import cn.icare.cms.PositionService;
import cn.icare.cms.aop.UserLog;
import cn.icare.cms.model.ro.AddPositionRO;
import cn.icare.cms.model.ro.EditPositionRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/position")
@Api(tags = "REST岗位")
public class PositionController extends BaseController {
    @Resource
    private PositionService userGroupService;

    @Resource
    private MenuService menuService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/usergroup
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")
    @UserLog(desc = "新增单个岗位")
    @ApiOperation(value = "新增单个岗位", notes = "t_position表新增单个岗位")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddPositionRO ro) {
        Long id = this.userGroupService.createOne(ro);
        if (id >= 0) {
            //设置岗位权限
            if(ro.getMenuIds()!=null && ro.getMenuIds().size()>0)
            {
                menuService.updateUserPositionMenu(id.intValue(),ro.getMenuIds());
            }
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/usergroup
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @UserLog(desc = "更新单个岗位")
    @ApiOperation(value = "更新单个岗位", notes = "usergroup表更新单个岗位")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditPositionRO ro) {
        if (this.userGroupService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/usergroup/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @UserLog(desc = "删除单个岗位")
    @ApiOperation(value = "删除单个岗位", notes = "usergroup表删除符合主键的单个岗位")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.userGroupService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/usergroup?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @UserLog(desc = "删除多个岗位")
    @ApiOperation(value = "删除多个岗位", notes = "usergroup表删除符合主键的多个岗位")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.userGroupService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/usergroup/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个岗位", notes = "usergroup表返回符合主键的单个岗位")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.userGroupService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/usergroup?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个岗位", notes = "user表返回符合条件的多个岗位")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize) {
        return super.getRespOk(this.userGroupService.getList(name, pageNum, pageSize));
    }

    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddPositionRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditPositionRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(String name, Integer pageNum, Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion
}
